## ---- simulate-diffusion ----

library(parallel)
library(igraph)
library(data.table)

load("replication_file_01_simulated_net.RData")

no_cores <- detectCores() - 1
cl <- makeCluster(no_cores)

fun4Parallel <- function(j, sc_bin, internet_bin, save_plots = FALSE) {
  
  library(igraph)
  library(data.table)
  
  g <- randomSocialNetwork(n_communities = 20, g_size = 490, min_comm_size = 15, 
                           within_p = .115, between_p = 0.00005)
  g_lo <- layout.fruchterman.reingold(g)
  
  # Node attributes
  V(g)$political_distress <- trunctedProp(vcount(g), mean=.5, sd = .2)
  V(g)$internal_drive_wtout_internet <- rbinom(vcount(g), 1, p = 0.1) 
  V(g)$internal_drive_wt_internet <- rbinom(vcount(g), 1, p = 0.5)  
  V(g)$relational_drive <- trunctedProp(vcount(g), mean=.2, sd = .2)
  
  # Edge attributes
  E(g)$weight_sc <- trunctedProp(ecount(g), mean=.6, sd = .15)
  E(g)$weight_nosc <- trunctedProp(ecount(g), mean=.2, sd = .15)
  
  # Penalise cross-community edges
  es <- as_edgelist(g)
  es[,1] <- gsub("\\d", "", es[,1]) 
  es[,2] <- gsub("\\d", "", es[,2]) 
  is_same_comm <- es[,1] == es[,2]
  
  E(g)$weight_nosc[!is_same_comm] <- 
    trunctedProp(sum(!is_same_comm), mean=0, sd = .15)
  
  # Initialise
  V(g)$active <- FALSE
  
  V(g)$active <- FALSE
  
  # The network structure (edges) is determined by `internal_drive_wtout_internet` also
  # for the Internet scenario.
  V(g)$internal_drive <- V(g)$internal_drive_wtout_internet
  
  if (sc_bin == TRUE) {
    E(g)$weight <- E(g)$weight_sc
    g <- addSCNet(g)
  } else {
    E(g)$weight <- E(g)$weight_nosc
  }
  
  if (internet_bin == TRUE) {
    V(g)$internal_drive <- V(g)$internal_drive_wt_internet
  } else {
    V(g)$internal_drive <- V(g)$internal_drive_wtout_internet
  }
  
  what <- ifelse(sc_bin, "wt sc", "wtout sc")
  path <- ifelse(sc_bin, "sim_wt_sc", "sim_wtout_sc")
  if (internet_bin == TRUE) {
    what <- paste0(what, " wt internet")
    path <- paste0(path, "_wt_internet/")
  } else {
    what <- paste0(what, " wtout internet")
    path <- paste0(path, "_wtout_internet/")
  }
  this_dt <- data.table()
  for (i in 1:1000) {
    res <- doNetSim(i, g)
    g <- res$graph
    if (save_plots == TRUE) {
      jpeg(file = paste0(path, sprintf('%03d.jpg', i)),
           width = 2500, height = 2500)
      par(mar=c(0,0,4,0))
      plot(g, vertex.label = NA, vertex.size = 4, layout = g_lo,
           vertex.color = ifelse(V(g)$active, "black", "white"))
      title(main = paste0("Time: ", i, 
                          "     Activation: ", 
                          round(sum(V(g)$active) / vcount(g) *100, 2), 
                          "%"),
            cex.main = 6)
      dev.off()
    } 
    this_dt <- rbind(this_dt,
                     data.table(activation = res$activation,
                                t = i,
                                j,
                                what = what))
  }
  return(this_dt)
}
X <- lsf.str()
clusterExport(cl=cl, varlist=c(as.vector(X)), envir=environment())

print('active_nodes_wt_sc_wtout_internet')
active_nodes_wt_sc_wtout_internet <- 
  rbindlist(parLapply(cl, 1:100, fun4Parallel, sc_bin = T, 
                      internet_bin = F, save_plots = FALSE))
# ffmpeg -framerate 10 -i %03d.jpg -c:v libx264 -profile:v high -crf 20 -pix_fmt yuv420p 
# sim_g_wt_sc_wtout_internet.mp4 

print('active_nodes_wtout_sc_wtout_internet')
active_nodes_wtout_sc_wtout_internet <- 
  rbindlist(parLapply(cl, 1:100, fun4Parallel, sc_bin = F, 
                      internet_bin = F, save_plots = FALSE))
# ffmpeg -framerate 10 -i %03d.jpg -c:v libx264 -profile:v high -crf 20 -pix_fmt yuv420p 
# sim_g_wtout_sc_wtout_internet.mp4 

print('active_nodes_wt_sc_wt_internet')
active_nodes_wt_sc_wt_internet <- 
  rbindlist(parLapply(cl, 1:100, fun4Parallel, sc_bin = T, 
                      internet_bin = T, save_plots = FALSE))

print('active_nodes_wtout_sc_wt_internet')
active_nodes_wtout_sc_wt_internet <- 
  rbindlist(parLapply(cl, 1:100, fun4Parallel, sc_bin = F, 
                      internet_bin = T, save_plots = FALSE))

stopCluster(cl)

save(active_nodes_wt_sc_wtout_internet, active_nodes_wtout_sc_wtout_internet, 
     active_nodes_wt_sc_wt_internet, active_nodes_wtout_sc_wt_internet, 
     file = 'replication_file_02_active_nodes.RData')